home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp48_2 / obj.pos < prev    next >
Text File  |  1995-03-23  |  7KB  |  162 lines

  1. Article 1853 of comp.sys.handhelds:
  2. Path: en.ecn.purdue.edu!pur-ee!mentor.cc.purdue.edu!purdue!tut.cis.ohio-state.edu!snorkelwacker!usc!elroy.jpl.nasa.gov!jarthur!nntp-server.caltech.edu!piglet!madler
  3. From: madler@piglet.caltech.edu (Mark Adler)
  4. Newsgroups: comp.sys.handhelds
  5. Subject: Re: My last post on sorting (sure) for the HP-48SX
  6. Message-ID: <1990Apr16.194958.22624@laguna.ccsf.caltech.edu>
  7. Date: 16 Apr 90 19:49:58 GMT
  8. References: <1990Apr11.045126.5737@laguna.ccsf.caltech.edu> <54098@microsoft.UUCP>
  9. Sender: news@laguna.ccsf.caltech.edu
  10. Organization: California Institute of Technology, Pasadena
  11. Lines: 147
  12.  
  13.  
  14. Oops on OPOS.  Here is a corrected version of OPOS---the last one
  15. didn't work for elements that belonged on the end of the list.
  16.  
  17. Also included here is an improved QPART using Alonzo's code for
  18. swapping [i] and [j] more efficiently.  (I see that Alonzo can't
  19. resist small tweaks either.)
  20.  
  21. Mark Adler
  22. madler@tybalt.caltech.edu
  23.  
  24.  
  25. %%HP: T(3)A(R)F(.);
  26. @ OPOS crc #DEh length 142
  27. @
  28. @ by Mark Adler  16 Apr 1990
  29. @
  30. @ Ordered POS: the arguments are a list in level 2 and the element to
  31. @ locate in level 1.  The result is the location in level 2 and a
  32. @ boolean in level 1 which is true if the element is in the list.  The
  33. @ location has the range 1..n+1, where n is the number of entries in
  34. @ the list.  n+1 indicates the element belongs at the end of the list
  35. @ (the boolean is always false in this case).  It is assumed that the
  36. @ input list is ordered so that the first entry is less than or equal
  37. @ to the second entry, the second is less than or equal to the third,
  38. @ etc.
  39. @
  40. @ OPOS uses SRCH to find where in the list the element goes.  SRCH
  41. @ uses the ">" operation to compare elements, but this can be changed
  42. @ in SRCH---see the comments in SRCH.  The ">" in this routine would
  43. @ also have to be changed in the same way.
  44. @
  45. \<<
  46.   SWAP OBJ\-> \-> n
  47.   \<<
  48.     n 1 + ROLL n SRCH                   @ do search
  49.     3 - \-> k j                         @ save k, index
  50.     \<<
  51.       IF j DUP THEN                     @ if j not past end,
  52.         PICK k > NOT                    @  see if k in list
  53.       END
  54.       n 1 + ROLLD n DROPN               @ trash list
  55.       n j - 1 +                         @ compute location
  56.       SWAP                              @ return index, boolean
  57.     \>>
  58.   \>>
  59. \>>
  60.  
  61.  
  62. %%HP: T(3)A(R)F(.);
  63. @ QPART crc #156h length 412
  64. @
  65. @ by Mark Adler  16 Apr 1990
  66. @ Given a partition of the stack, pick an entry in the partition and
  67. @ split the partition into two partitions such that all the entries in
  68. @ the first one are less than the entry picked, and all the entries in
  69. @ the second one are greater than the entry picked, and the entry is
  70. @ placed between them.  Then call this routine recursively for each of
  71. @ those partitions, unless the partition is 20 entries or less.  A
  72. @ final pass of an insertion sort should be done to sort the remaining
  73. @ short partitions.  The value of 20 was determined experimentally on
  74. @ test cases of lists of random reals.
  75. @
  76. @ The entry for partitioning is picked using the median method, which
  77. @ picks the median value of the entries at the beginning, middle, and
  78. @ end of the list.  This makes the sort fast for already sorted lists,
  79. @ and greatly reduces the probability of the sort taking order n^2
  80. @ time.  It also reduces the total number of comparisons, speeding the
  81. @ sort somewhat.  Most importantly, an additional step of sorting the
  82. @ first and last elements of the list (which adds one compare) allows
  83. @ the inner loops of the algorithm to not have to check for the bounds
  84. @ of the partition.  This special-purpose three element sort does not
  85. @ cost extra, since the outer two elements would have to have been
  86. @ compared and swapped anyway if they were in the wrong place.  Thanks
  87. @ to Alonzo Gariepy for an improved swap of [i] and [j].
  88. @
  89. @ The arguments to QPART are two integers specifying the stack levels
  90. @ to partition.  The arguments refer to the stack after the arguments
  91. @ are removed from the stack.  The integer in the first level minus 1
  92. @ is the starting level, and the integer in level 2 is the ending level
  93. @ to partition.  So, for example, to partition the stack levels 1
  94. @ through n, the calling sequence would be "n 2 QPART".  Since QPART
  95. @ does not check the partition size on entry, the calling routine
  96. @ should do that and avoid calling QPART for sizes of 20 or less.
  97. @
  98. @ The algorithm can be modified to sort objects besides reals and
  99. @ strings by replacing the ">" in the "IF DUP2 >"'s and in the "PICK >"
  100. @ and replacing the "<" in the "PICK <" with the appropriate code for
  101. @ the object.  The routine can be generalized (at a speed penalty) by
  102. @ replacing said ">"'s with "GT" and the said "<" with "SWAP GT" and
  103. @ defining a function "GT" that does the job before doing the sort.
  104. @
  105. \<<
  106.   @ sort the partition of the stack from level l-1 to level r (after
  107.   @  l and r are pulled off of the stack).
  108.   \-> r l \<<
  109.     @
  110.     @ sort [l-1], [m], [r] so that [l-1] >= [m] >= [r] where m points
  111.     @  to the middle of the partition.
  112.     @
  113.     r l + 2 / FLOOR ROLL                @ get [m]
  114.     l ROLL                              @ get [l-1]
  115.     IF DUP2 > THEN SWAP END             @ sort [m], [l-1]
  116.     l ROLLD r ROLL                      @ put back new [l-1]; get [r]
  117.     IF DUP2 > THEN                      @ if [r], [m] sorted,
  118.       r                                 @  then just put [r] back
  119.     ELSE
  120.       SWAP r ROLLD l ROLL               @ else sort [r], [m]; get [l-1]
  121.       IF DUP2 > THEN SWAP END           @  and re-sort [m] and [l-1]
  122.       l                                 @ put [l-1] back
  123.     END
  124.     ROLLD
  125.     @
  126.     @ start sorting inside [l-1], [r] since [l-1] and [r] are already
  127.     @ sorted.  put [m] in list so that [i] >= [m] >= [j] for i < j.
  128.     @
  129.     r 3 + SWAP                          @ i = l-1 (loop begins "1 +")
  130.     l 3 +                               @ j = r-1 (since m pulled out)
  131.     WHILE                               @ stack is i+4,[m],j+4,list...
  132.       WHILE 1 + DUP2 PICK < REPEAT END  @ now [m] >= [i]
  133.       SWAP ROT
  134.       WHILE 1 - DUP2 PICK > REPEAT END  @ now [j] >= [m]
  135.       ROT DUP2 >                        @ until j <= i
  136.     REPEAT                              @ stack is i+4,j+4,[m],list...
  137.       DUP2 SWAP ROLL SWAP ROLLD         @ swap [i] and [j]
  138.       DUP2 ROLL OVER ROLLD
  139.       DROP ROT SWAP                     @ stack is i+4,[m],j+4,list...
  140.     END
  141.     DROP 2 - SWAP OVER ROLLD            @ put [m] after [j]
  142.     @
  143.     @ sort the partitions [j+2..r] and [l-1..j] by calling this program
  144.     @  recursively, but only if the partition has length > 20.  The
  145.     @  stack is j+2,list...
  146.     @
  147.     IF r DUP2 - -18 < THEN              @ check top partition
  148.       SWAP DUP 'r' STO 1 + QPART r      @ save j+2 in r
  149.     ELSE
  150.       DROP
  151.     END                                 @ j+2 left on stack
  152.     IF 2 - l DUP2 - 18 > THEN           @ check bottom partition
  153.       QPART
  154.     ELSE
  155.       DROP2
  156.     END                                 @ leave the list on the stack
  157.   \>>
  158. \>>
  159.  
  160.  
  161.